package com.glory.hikvisionmobile.activity;

import android.app.Activity;
import android.os.Parcelable;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.DataCollectDetailsAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.bean.DataCollect.DcModelBean;
import com.glory.hikvisionmobile.bean.DataCollect.XEdcItemSetLinesBean;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager;
import java.util.ArrayList;
import java.util.List;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * @author yi.zhang
 * @date 2019/12/9
 * @desc 数据采集详情
 */
public class DataCollectDetailsActivity extends BaseActivity {

    @BindView(R.id.details_Rv)
    RecyclerView detailsRv;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvRange)
    TextView tvRange;

    private DataCollectDetailsAdapter detailsAdapter;
    private List<XEdcItemSetLinesBean> xEdcItemSetLinesBeans;
    private int postion;
    private XEdcItemSetLinesBean bean;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_data_collect_details;
    }

    @Override
    protected void initView() {
        detailsRv.setLayoutManager(new LinearLayoutManager(this));
        detailsRv.setItemAnimator(new DefaultItemAnimator());
        detailsRv.addItemDecoration(new DividerItemDecoration(this, 1));
    }

    @Override
    protected void initData() {

        try {
            xEdcItemSetLinesBeans = getIntent().getParcelableArrayListExtra("xEdcItemSetLinesBeans");
            if(xEdcItemSetLinesBeans==null)
                return;
            postion = getIntent().getIntExtra("postion",0);
            bean = xEdcItemSetLinesBeans.get(postion);
            tvRange.setText(bean.getMinValue() + "/" + bean.getMidValue()+"/" + bean.getMaxValue());
            detailsAdapter = new DataCollectDetailsAdapter(R.layout.adapter_collectdetails,null,bean.getMinValue(),bean.getMaxValue());
            detailsAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            detailsAdapter.isFirstOnly(true);
            detailsRv.setAdapter(detailsAdapter);
            if(bean.getXPdaDcModels().size()==0)
                getWaferLoadList();
            detailsAdapter.setNewData(bean.getXPdaDcModels());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            showToast("数据异常",false);
        }
    }

    private void getWaferLoadList()
    {
        switch (bean.getEdcSampleType())
        {
            case "ITEM":
                String[] itemDescs = bean.getEdcItemDesc().split(";");
                for (int i = 0;i<bean.getEdcItemCount();i++)
                {
                    DcModelBean dcModelBean = new DcModelBean();
                    dcModelBean.setDcName(itemDescs[i]);
                    bean.getXPdaDcModels().add(dcModelBean);
                }
                break;
            case "COMP":
                String[] compDescs = bean.getEdcCompDesc().split(";");
                bean.getxPdaComponents().forEach(v->{
                    if(v.getIsSelected().equals("Y"))
                    {
                        for(int i = 0;i<bean.getEdccomponentCount();i++)
                        {
                            DcModelBean dcModelBean = new DcModelBean();
                            dcModelBean.setDcName(v.getComponentId()+"-"+compDescs[i]);
                            bean.getXPdaDcModels().add(dcModelBean);
                        }
                    }
                });
                break;
            case "SITE":
                String[] siteDescs = bean.getEdcSiteDesc().split(";");
                bean.getxPdaComponents().forEach(v->{
                    if(v.getIsSelected().equals("Y"))
                    {
                        for(int i = 1;i<=bean.getEdccomponentCount();i++)
                        {
                            for(int j = 0;j<bean.getEdcSiteCount();j++)
                            {
                                DcModelBean dcModelBean = new DcModelBean();
                                dcModelBean.setDcName(v.getComponentId()+"-"+i+"-"+siteDescs[j]);
                                bean.getXPdaDcModels().add(dcModelBean);
                            }
                        }
                    }
                });
                break;
        }
    }

    private boolean checkData()
    {
        if(bean.getIsMandatory().equals("Y"))
        {
            for (DcModelBean dcModelBean : bean.getXPdaDcModels()) {
                if(dcModelBean.getDcData().equals("")){
                    showToast("有未完成填写项，请填写完成",false);
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("数据采集");
        freeUI_titleView.setRightListener("完成",v->{
            if(checkData()){
                getIntent().putParcelableArrayListExtra("xEdcItemSetLinesBeans", (ArrayList<? extends Parcelable>) xEdcItemSetLinesBeans);
                setResult(Activity.RESULT_OK, getIntent());
                FreeApi_ActivityManager.getActivityManager().finishActivity();
            }
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }
}
